****************************************************************************
**Win or Lose: Residential Sorting After a School Choice Lottery
*By Andrew Bibler & Stephen B Billings

****************************************************************************

clear all
set more off

foreach year in 08 09 10 11 12 13 14 15 {  

use mb_20`year'_pub.dta , clear  

gen year = 20`year'   

keep if mastid!=.  

keep if (grade=="03" | grade=="04" | grade=="05" | grade=="06" | grade=="07" | grade=="08" )  
	
gen grade_r = real(grade)  
tab grade_r grade  
drop grade  
rename grade_r grade  
	
if year<2008 {  

gen ma_valid = (regexm(ma_test_id, ".*MA") & (grade>=3) & (grade<=8))  
gen rd_valid = (regexm(rd_test_id, ".*RD") & (grade>=3) & (grade<=8))  
	
cap noisily gen ma_score_rg_r = real(ma_score) if regexm(ma_test_id, ".*MA")  
cap noisily gen ma_score_rg_r = ma_score if regexm(ma_test_id, ".*MA")  

cap noisily gen rd_score_rg_r = real(rd_score) if regexm(rd_test_id, ".*RD")  
cap noisily gen rd_score_rg_r = rd_score if regexm(rd_test_id, ".*RD")  

replace rd_score_rg_r=-1 if rd_score_rg_r==.  
replace ma_score_rg_r=-1 if ma_score_rg_r==.   

bysort mastid grade: egen maxrd_score = max(ma_score_rg_r)  
bysort mastid grade: egen maxma_score = max(ma_score_rg_r)  

cap noisily gen ma_level_rg = real(ma_ach_level) if regexm(ma_test_id, ".*MA")  
cap noisily gen ma_level_rg = ma_ach_level if regexm(ma_test_id, ".*MA")  

cap noisily gen rd_level_rg = real(rd_ach_level) if regexm(rd_test_id, ".*RD")  
cap noisily gen rd_level_rg = rd_ach_level if regexm(rd_test_id, ".*RD")  

replace rd_level_rg=-1 if rd_level_rg==.  
replace ma_level_rg=-1 if ma_level_rg==.   

bysort mastid grade: egen maxrd_level = max(rd_level_rg)  
bysort mastid grade: egen maxma_level = max(ma_level_rg)  

gen read = maxrd_score  
gen math = maxma_score  

gen read_grlevel = maxrd_level>=3  
gen math_grlevel = maxma_level>=3  		

duplicates tag mastid grade , gen(dups)  

replace read=. if read==-1  
replace math=. if math==-1  

replace read_grlevel=. if maxrd_level==-1  
replace math_grlevel=. if maxma_level==-1   

duplicates drop mastid grade , force  

duplicates report mastid grade  

	}  
	

if year == 2008 {  

gen ma_valid = (regexm(ma_test_id, ".*MA") & (grade>=3) & (grade<=8))  
gen rd_valid = (regexm(rd_test_id, ".*RD") & (grade>=3) & (grade<=8))  
gen sc_valid = (regexm(sc_test_id, ".*SC") & (grade==5) | (grade==8))  	

cap noisily gen ma_score_rg_r = real(ma_score) if regexm(ma_test_id, ".*MA")  
cap noisily gen ma_score_rg_r = ma_score if regexm(ma_test_id, ".*MA")  

cap noisily gen rd_score_rg_r = real(rd_score) if regexm(rd_test_id, ".*RD")  
cap noisily gen rd_score_rg_r = rd_score if regexm(rd_test_id, ".*RD")  

cap noisily gen sc_score_rg_r = real(sc_score) if regexm(sc_test_id, ".*SC")  
cap noisily gen sc_score_rg_r = sc_score if regexm(sc_test_id, ".*SC")  

replace rd_score_rg_r=-1 if rd_score_rg_r==.  
replace ma_score_rg_r=-1 if ma_score_rg_r==.   
replace sc_score_rg_r=-1 if sc_score_rg_r==.   

bysort mastid grade: egen maxrd_score = max(ma_score_rg_r)  
bysort mastid grade: egen maxma_score = max(ma_score_rg_r)  
bysort mastid grade: egen maxsc_score = max(sc_score_rg_r)  
   
cap noisily gen ma_level_rg = real(ma_ach_level) if regexm(ma_test_id, ".*MA")  
cap noisily gen ma_level_rg = ma_ach_level if regexm(ma_test_id, ".*MA")  

cap noisily gen rd_level_rg = real(rd_ach_level) if regexm(rd_test_id, ".*RD")  
cap noisily gen rd_level_rg = rd_ach_level if regexm(rd_test_id, ".*RD")  

cap noisily gen sc_level_rg = real(sc_ach_level) if regexm(sc_test_id, ".*SC")  
cap noisily gen sc_level_rg = sc_ach_level if regexm(sc_test_id, ".*SC")  

replace rd_level_rg=-1 if rd_level_rg==.  
replace ma_level_rg=-1 if ma_level_rg==.   
replace sc_level_rg=-1 if sc_level_rg==.   

bysort mastid grade: egen maxrd_level = max(rd_level_rg)  
bysort mastid grade: egen maxma_level = max(ma_level_rg)  
bysort mastid grade: egen maxsc_level = max(sc_level_rg)  

gen read = maxrd_score  
gen math = maxma_score  
gen science = maxsc_score  

gen read_grlevel = maxrd_level>=3  
gen math_grlevel = maxma_level>=3  		
gen sci_grlevel = maxsc_level>=3  

duplicates tag mastid grade , gen(dups)  

replace read=. if read==-1  
replace math=. if math==-1  
replace science=. if math==-1  

replace read_grlevel=. if maxrd_level==-1  
replace math_grlevel=. if maxma_level==-1   
replace sci_grlevel=. if maxsc_level==-1   

duplicates drop mastid grade , force  

duplicates report mastid grade  

	}  
	
if year == 2009 {  

gen ma_rg_valid = (regexm(ma_rg_test_id, ".*MA") & (grade>=3) & (grade<=8))  
gen ma_r1_valid = (regexm(ma_r1_test_id, ".*MA") & (grade>=3) & (grade<=8))  
gen rd_valid = (regexm(rd_test_id, ".*RD") & (grade>=3) & (grade<=8))  
gen sc_valid = (regexm(sc_test_id, ".*SC") & (grade==5 | grade==8))  
	
cap noisily gen ma_score_rg_r = real(ma_rg_score) if regexm(ma_rg_test_id, ".*MA")  
cap noisily gen ma_score_rg_r = ma_rg_score if regexm(ma_rg_test_id, ".*MA")  

cap noisily gen ma_score_r1_r = real(ma_r1_score) if regexm(ma_r1_test_id, ".*MA")  
cap noisily gen ma_score_r1_r = ma_r1_score if regexm(ma_r1_test_id, ".*MA")  

cap noisily gen rd_score_rg_r = real(rd_rg_score) if regexm(rd_test_id, ".*RD")  
cap noisily gen rd_score_rg_r = rd_rg_score if regexm(rd_test_id, ".*RD")  

cap noisily gen rd_score_r1_r = real(rd_r1_score) if regexm(rd_test_id, ".*RD")  
cap noisily gen rd_score_r1_r = rd_r1_score if regexm(rd_test_id, ".*RD")  

cap noisily gen sc_score_rg_r = real(sc_rg_score) if regexm(sc_test_id, ".*SC")  
cap noisily gen sc_score_rg_r = sc_rg_score if regexm(sc_test_id, ".*SC")  	

cap noisily gen sc_score_r1_r = real(sc_r1_score) if regexm(sc_test_id, ".*SC")  
cap noisily gen sc_score_r1_r = sc_r1_score if regexm(sc_test_id, ".*SC")  	


egen maxma = rowmax(ma_score_rg_r ma_score_r1_r)  
egen maxrd = rowmax(rd_score_rg_r rd_score_r1_r)  
egen maxsc = rowmax(sc_score_rg_r sc_score_r1_r)  

replace maxrd=-1 if maxrd==.  
replace maxma=-1 if maxma==.   
replace maxsc=-1 if maxsc==.  

bysort mastid grade: egen maxrd_score = max(maxrd)  
bysort mastid grade: egen maxma_score = max(maxma)  
bysort mastid grade: egen maxsc_score = max(maxsc)  

cap noisily gen ma_level_rg = real(ma_rg_ach_level) if regexm(ma_rg_test_id, ".*MA")  
cap noisily gen ma_level_rg = ma_rg_ach_level if regexm(ma_rg_test_id, ".*MA")  

cap noisily gen ma_level_r1 = real(ma_r1_ach_level) if regexm(ma_r1_test_id, ".*MA")  
cap noisily gen ma_level_r1 = ma_r1_ach_level if regexm(ma_r1_test_id, ".*MA")  

cap noisily gen rd_level_rg = real(rd_rg_ach_level) if regexm(rd_test_id, ".*RD")  
cap noisily gen rd_level_rg = rd_rg_ach_level if regexm(rd_test_id, ".*RD")  

cap noisily gen rd_level_r1 = real(rd_r1_ach_level) if regexm(rd_test_id, ".*RD")  
cap noisily gen rd_level_r1 = rd_r1_ach_level if regexm(rd_test_id, ".*RD")  

cap noisily gen sc_level_rg = real(sc_rg_ach_level) if regexm(sc_test_id, ".*SC")  
cap noisily gen sc_level_rg = sc_rg_ach_level if regexm(sc_test_id, ".*SC")  	

cap noisily gen sc_level_r1 = real(sc_r1_ach_level) if regexm(sc_test_id, ".*SC")  
cap noisily gen sc_level_r1 = sc_r1_ach_level if regexm(sc_test_id, ".*SC")  	

egen maxma_lev = rowmax(ma_level_rg ma_level_r1)  
egen maxrd_lev = rowmax(rd_level_rg rd_level_r1)  
egen maxsc_lev = rowmax(sc_level_rg sc_level_r1)    

replace maxrd_lev=-1 if maxrd_lev==.  
replace maxma_lev=-1 if maxma_lev==.   
replace maxsc_lev=-1 if maxsc_lev==.  

bysort mastid grade: egen maxrd_level = max(maxrd_lev)  
bysort mastid grade: egen maxma_level = max(maxma_lev)  
bysort mastid grade: egen maxsc_level = max(maxsc_lev)  

gen read = maxrd_score  
gen math = maxma_score  
gen science = maxsc_score  

replace read=. if read==-1  
replace math=. if math==-1  
replace science=. if science==-1  

gen read_grlevel = maxrd_level>=3  
gen math_grlevel = maxma_level>=3  		
gen sci_grlevel = maxsc_level>=3  

replace read_grlevel=. if maxrd_level==-1  
replace math_grlevel=. if maxma_level==-1   
replace sci_grlevel=. if maxsc_level==-1  

duplicates tag mastid grade , gen(dups)  

duplicates drop mastid grade , force  

duplicates report mastid grade  

	}  

	
if year>=2010 & year<=2012 {  

gen ma_rg_valid = (regexm(ma_rg_test_id, ".*MA") & (grade>=3) & (grade<=8))  
gen ma_r1_valid = (regexm(ma_r1_test_id, ".*MA") & (grade>=3) & (grade<=8))  
gen rd_rg_valid = (regexm(rd_rg_test_id, ".*RD") & (grade>=3) & (grade<=8))  
gen rd_r1_valid = (regexm(rd_r1_test_id, ".*RD") & (grade>=3) & (grade<=8))  
gen sc_rg_valid = (regexm(sc_rg_test_id, ".*SC") & (grade==5 | grade==8))  
gen sc_r1_valid = (regexm(sc_r1_test_id, ".*SC") & (grade==5 | grade==8))  
	
cap noisily gen ma_score_rg_r = real(ma_rg_score) if regexm(ma_rg_test_id, ".*MA")  
cap noisily gen ma_score_rg_r = ma_rg_score if regexm(ma_rg_test_id, ".*MA")  

cap noisily gen ma_score_r1_r = real(ma_r1_score) if regexm(ma_r1_test_id, ".*MA")  
cap noisily gen ma_score_r1_r = ma_r1_score if regexm(ma_r1_test_id, ".*MA")  

cap noisily gen rd_score_rg_r = real(rd_rg_score) if regexm(rd_rg_test_id, ".*RD")  
cap noisily gen rd_score_rg_r = rd_rg_score if regexm(rd_rg_test_id, ".*RD")  

cap noisily gen rd_score_r1_r = real(rd_r1_score) if regexm(rd_r1_test_id, ".*RD")  
cap noisily gen rd_score_r1_r = rd_r1_score if regexm(rd_r1_test_id, ".*RD")  

cap noisily gen sc_score_rg_r = real(sc_rg_score) if regexm(sc_rg_test_id, ".*SC")  
cap noisily gen sc_score_rg_r = sc_rg_score if regexm(sc_rg_test_id, ".*SC")  	

cap noisily gen sc_score_r1_r = real(sc_r1_score) if regexm(sc_r1_test_id, ".*SC")  
cap noisily gen sc_score_r1_r = sc_r1_score if regexm(sc_r1_test_id, ".*SC")  	


egen maxma = rowmax(ma_score_rg_r ma_score_r1_r)  
egen maxrd = rowmax(rd_score_rg_r rd_score_r1_r)  
egen maxsc = rowmax(sc_score_rg_r sc_score_r1_r)  

replace maxrd=-1 if maxrd==.  
replace maxma=-1 if maxma==.   
replace maxsc=-1 if maxsc==.  

bysort mastid grade: egen maxrd_score = max(maxrd)  
bysort mastid grade: egen maxma_score = max(maxma)  
bysort mastid grade: egen maxsc_score = max(maxsc)  

cap noisily gen ma_level_rg = real(ma_rg_ach_level) if regexm(ma_rg_test_id, ".*MA")  
cap noisily gen ma_level_rg = ma_rg_ach_level if regexm(ma_rg_test_id, ".*MA")  

cap noisily gen ma_level_r1 = real(ma_r1_ach_level) if regexm(ma_r1_test_id, ".*MA")  
cap noisily gen ma_level_r1 = ma_r1_ach_level if regexm(ma_r1_test_id, ".*MA")  

cap noisily gen rd_level_rg = real(rd_rg_ach_level) if regexm(rd_rg_test_id, ".*RD")  
cap noisily gen rd_level_rg = rd_rg_ach_level if regexm(rd_rg_test_id, ".*RD")  

cap noisily gen rd_level_r1 = real(rd_r1_ach_level) if regexm(rd_r1_test_id, ".*RD")  
cap noisily gen rd_level_r1 = rd_r1_ach_level if regexm(rd_r1_test_id, ".*RD")  

cap noisily gen sc_level_rg = real(sc_rg_ach_level) if regexm(sc_rg_test_id, ".*SC")  
cap noisily gen sc_level_rg = sc_rg_ach_level if regexm(sc_rg_test_id, ".*SC")  	

cap noisily gen sc_level_r1 = real(sc_r1_ach_level) if regexm(sc_r1_test_id, ".*SC")  
cap noisily gen sc_level_r1 = sc_r1_ach_level if regexm(sc_r1_test_id, ".*SC")  	

egen maxma_lev = rowmax(ma_level_rg ma_level_r1)  
egen maxrd_lev = rowmax(rd_level_rg rd_level_r1)  
egen maxsc_lev = rowmax(sc_level_rg sc_level_r1)    

replace maxrd_lev=-1 if maxrd_lev==.  
replace maxma_lev=-1 if maxma_lev==.   
replace maxsc_lev=-1 if (maxsc_lev==. & year>=2008)  

bysort mastid grade: egen maxrd_level = max(maxrd_lev)  
bysort mastid grade: egen maxma_level = max(maxma_lev)  
bysort mastid grade: egen maxsc_level = max(maxsc_lev)  

gen read = maxrd_score  
gen math = maxma_score  
gen science = maxsc_score  

replace read=. if read==-1  
replace math=. if math==-1  
replace science=. if science==-1  

gen read_grlevel = maxrd_level>=3  
gen math_grlevel = maxma_level>=3  		
gen sci_grlevel = maxsc_level>=3  

replace read_grlevel=. if maxrd_level==-1  
replace math_grlevel=. if maxma_level==-1   
replace sci_grlevel=. if maxsc_level==-1  

duplicates tag mastid grade , gen(dups)  

duplicates drop mastid grade , force  

duplicates report mastid grade  

	}  

if year>=2013 {  

gen ma_rg_valid = (regexm(pc_ma_type , ".*RG") & (grade>=3) & (grade<=8))  
gen ma_r1_valid = (regexm(pc_ma_type , ".*X1") & (grade>=3) & (grade<=8))  
gen rd_rg_valid = (regexm(pc_rd_type , ".*RG") & (grade>=3) & (grade<=8))  
gen rd_r1_valid = (regexm(pc_rd_type , ".*X1") & (grade>=3) & (grade<=8))  
gen sc_rg_valid = (regexm(pc_sc_type , ".*RG") & (grade==5 | grade==8))  
gen sc_r1_valid = (regexm(pc_sc_type , ".*X1") & (grade==5 | grade==8))  
	
cap noisily gen ma_score_rg_r = real(pc_ma_score) if ma_rg_valid==1  
cap noisily gen ma_score_rg_r = pc_ma_score if  ma_rg_valid==1   

cap noisily gen ma_score_r1_r = real(pc_ma_score) if  ma_r1_valid==1   
cap noisily gen ma_score_r1_r = pc_ma_score if  ma_r1_valid==1   

cap noisily gen rd_score_rg_r = real(pc_rd_score) if  rd_rg_valid==1   
cap noisily gen rd_score_rg_r = pc_rd_score if  rd_rg_valid==1   

cap noisily gen rd_score_r1_r = real(pc_rd_score) if rd_r1_valid==1  
cap noisily gen rd_score_r1_r = pc_rd_score if rd_r1_valid==1  

cap noisily gen sc_score_rg_r = real(pc_sc_score) if sc_rg_valid==1  
cap noisily gen sc_score_rg_r = pc_sc_score if sc_rg_valid==1  	

cap noisily gen sc_score_r1_r = real(pc_sc_score) if sc_r1_valid==1  
cap noisily gen sc_score_r1_r = pc_sc_score if sc_r1_valid==1  	

egen maxma = rowmax(ma_score_rg_r ma_score_r1_r)  
egen maxrd = rowmax(rd_score_rg_r rd_score_r1_r)  
egen maxsc = rowmax(sc_score_rg_r sc_score_r1_r)  

replace maxrd=-1 if maxrd==.  
replace maxma=-1 if maxma==.   
replace maxsc=-1 if maxsc==.  


bysort mastid grade: egen maxrd_score = max(maxrd)  
bysort mastid grade: egen maxma_score = max(maxma)  
bysort mastid grade: egen maxsc_score = max(maxsc)  

cap noisily gen ma_level_rg = real(pc_ma_level) if ma_rg_valid==1  
cap noisily gen ma_level_rg = pc_ma_level if ma_rg_valid==1  

cap noisily gen ma_level_r1 = real(pc_ma_level) if ma_r1_valid==1  
cap noisily gen ma_level_r1 = pc_ma_level if ma_r1_valid==1  

cap noisily gen rd_level_rg = real(pc_rd_level) if rd_rg_valid==1  
cap noisily gen rd_level_rg = pc_rd_level if rd_rg_valid==1  

cap noisily gen rd_level_r1 = real(pc_rd_level) if rd_r1_valid==1  
cap noisily gen rd_level_r1 = pc_rd_level if rd_r1_valid==1  

cap noisily gen sc_level_rg = real(pc_sc_level) if sc_rg_valid==1  
cap noisily gen sc_level_rg = pc_sc_level if sc_rg_valid==1  	

cap noisily gen sc_level_r1 = real(pc_sc_level) if sc_r1_valid==1  
cap noisily gen sc_level_r1 = pc_sc_level if sc_r1_valid==1  	

egen maxma_lev = rowmax(ma_level_rg ma_level_r1)  
egen maxrd_lev = rowmax(rd_level_rg rd_level_r1)  
egen maxsc_lev = rowmax(sc_level_rg sc_level_r1)    

replace maxrd_lev=-1 if maxrd_lev==.  
replace maxma_lev=-1 if maxma_lev==.   
replace maxsc_lev=-1 if (maxsc_lev==. & year>=2008)  

bysort mastid grade: egen maxrd_level = max(maxrd_lev)  
bysort mastid grade: egen maxma_level = max(maxma_lev)  
bysort mastid grade: egen maxsc_level = max(maxsc_lev)  

gen read = maxrd_score  
gen math = maxma_score  
gen science = maxsc_score  

replace read=. if read==-1  
replace math=. if math==-1  
replace science=. if science==-1  

gen rd_ach_level = maxrd_level  
gen ma_ach_level = maxma_level  
gen sc_ach_level = maxsc_level  

gen read_grlevel = maxrd_level>=3  
gen math_grlevel = maxma_level>=3  		
gen sci_grlevel = maxsc_level>=3  

replace read_grlevel=. if maxrd_level==-1  
replace math_grlevel=. if maxma_level==-1   
replace sci_grlevel=. if maxsc_level==-1  

duplicates tag mastid grade , gen(dups)  

duplicates drop mastid grade , force  

duplicates report mastid grade  

	}  
	

gen mathstand=.  
gen readstand=.  
gen sciencestand=.  

su math if grade==3  
scalar ma_mn3 = r(mean)  
scalar ma_sd3 = r(sd)  

su math if grade==4  
scalar ma_mn4 = r(mean)  
scalar ma_sd4 = r(sd)  

su math if grade==5  
scalar ma_mn5 = r(mean)  
scalar ma_sd5 = r(sd)  

su math if grade==6  
scalar ma_mn6 = r(mean)  
scalar ma_sd6 = r(sd)  

su math if grade==7  
scalar ma_mn7 = r(mean)  
scalar ma_sd7 = r(sd)  

su math if grade==8  
scalar ma_mn8 = r(mean)  
scalar ma_sd8 = r(sd)  

su read if grade==3  
scalar rd_mn3 = r(mean)  
scalar rd_sd3 = r(sd)  

su read if grade==4  
scalar rd_mn4 = r(mean)  
scalar rd_sd4 = r(sd)  

su read if grade==5  
scalar rd_mn5 = r(mean)  
scalar rd_sd5 = r(sd)  

su read if grade==6  
scalar rd_mn6 = r(mean)  
scalar rd_sd6 = r(sd)  

su read if grade==7  
scalar rd_mn7 = r(mean)  
scalar rd_sd7 = r(sd)  

su read if grade==8  
scalar rd_mn8 = r(mean)  
scalar rd_sd8 = r(sd)  

replace mathstand = ((math - ma_mn3)/ma_sd3) if grade==3  
replace mathstand = ((math - ma_mn4)/ma_sd4) if grade==4  
replace mathstand = ((math - ma_mn5)/ma_sd5) if grade==5  
replace mathstand = ((math - ma_mn6)/ma_sd6) if grade==6  
replace mathstand = ((math - ma_mn7)/ma_sd7) if grade==7  
replace mathstand = ((math - ma_mn8)/ma_sd8) if grade==8  

replace readstand = ((read - rd_mn3)/rd_sd3) if grade==3  
replace readstand = ((read - rd_mn4)/rd_sd4) if grade==4  
replace readstand = ((read - rd_mn5)/rd_sd5) if grade==5  
replace readstand = ((read - rd_mn6)/rd_sd6) if grade==6  
replace readstand = ((read - rd_mn7)/rd_sd7) if grade==7  
replace readstand = ((read - rd_mn8)/rd_sd8) if grade==8  


bysort grade: su mathstand readstand  

if year>=2008 {  
su science if grade==5  
scalar sc_mn5 = r(mean)  
scalar sc_sd5 = r(sd)  

su science if grade==8  
scalar sc_mn8 = r(mean)  
scalar sc_sd8 = r(sd)  

replace sciencestand = ((science - sc_mn5)/sc_sd5) if grade==5  
replace sciencestand = ((science - sc_mn8)/sc_sd8) if grade==8  

bysort grade: su sciencestand  

	}  



gen cms = lea == "600"  

gen charter = schlcode == "000"  

gen frl_mb = .  

if (year==2006 | year==2007) {  
replace frl_mb = 1 if (frl=="F" | frl=="R")  
replace frl_mb = 0 if (frl=="N" | frl=="T")  
}  

if year>2007 {  
replace frl_mb = 1 if (eds == "Y")  
replace frl_mb = 0 if (eds == "N")  
}  

gen ma_aig = 0  
gen rd_aig = 0  
	

if (`year' == 09 | `year' == 10 | `year' == 11 | `year' == 12 ) {  
	replace ma_aig = 1 if aig_m == "Y"   
	replace ma_aig = . if (aig_m != "Y" & aig_m != "N")  
	
	replace rd_aig = 1 if aig_r == "Y"   
	replace rd_aig = . if (aig_r != "Y" & aig_r != "N")  
	}  


		
if year<=2008 {  
gen lep_Y = (lep_current=="Y")  
			}  
			
if year>2008 {  
gen lep_Y = (lep=="Y" | lep=="1")  
			}  

tab lep_Y  
	
if year<2008 {  
gen science=.  
gen sci_grlevel=.  
gen sc_ach_level=.  
	}  


keep mastid grade sex mathstand readstand sciencestand lep_Y ///
		year grade cms charter schlcode lea frl_mb ethni* ///
		read_grlevel rd_ach_level math_grlevel ma_ach_level ///
		sci_grlevel sc_ach_level ma_aig rd_aig  
		
			
tempfile mb`year' 		
	save `mb`year'' , replace 
	
	
		} 
		
	

** append

use `mb08' , clear 

foreach year in 09 10 11 12 13 14 15 { 
	
	append using `mb`year'' , force 
		
	} 
	
	
duplicates tag mastid grade , gen(repeat) 

bysort mastid grade: egen firstyear = min(year) 

rename year mbyear 
rename grade testgrade 

cd "$results"

save nc_chars.dta , replace


use nc_chars.dta , clear

keep mastid testgrade frl_mb

keep if testgrade==3 

rename frl_mb frl_mb3 

keep mastid frl_mb3

duplicates drop mastid , force

save frl_mb3.dta, replace 

